package com.lei.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lei.eduservice.entity.EduTeacher;
import com.lei.eduservice.entity.vo.TeacherQuery;
import com.lei.eduservice.service.EduTeacherService;
import com.lei.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author 熊磊
 * @since 2020-05-24
 */
@Api("讲师管理")
@RestController
@RequestMapping("/edu/teacher")
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;


    //获取讲师列表
    // @GetMapping("findAll") 也是可以的
    @ApiOperation("获取讲师列表")
    @GetMapping("/findAll")
    public Result findAll(){
        return Result.ok().data("items",eduTeacherService.list());
    }

    //删除推荐使用DeleteMapping
    @ApiOperation("逻辑删除讲师")
    @DeleteMapping("/delete/{id}")
    public Result removeTeacher(@ApiParam(name = "id" ,value = "讲师Id",required = true) @PathVariable("id") String id ){
        if (eduTeacherService.removeById(id)){
            return Result.ok();
        } else {
            return Result.error();
        }
    }


    //分页查询讲师
    @ApiOperation("分页查询讲师")
    @GetMapping("/pageTeacher/{current}/{size}")
    public Result pageTeacher(
            @ApiParam(name = "current" ,value = "当前页") @PathVariable("current") Long current,
            @ApiParam(name = "size" ,value = "页面大小") @PathVariable("size") Long size) {
        Page<EduTeacher> eduTeacherPage = new Page<>(current,size);
        eduTeacherService.page(eduTeacherPage);

        long total = eduTeacherPage.getTotal();
        List<EduTeacher> records = eduTeacherPage.getRecords();
        return Result.ok().data("total",total).data("records",records);
    }


    //条件分页查询讲师
    @ApiOperation("条件分页查询讲师")
    //使用Requestbody需要页面以Post方式提交
    //RequestBody传递的是Json字符串
    @PostMapping("/pageTeacherQuery/{current}/{size}")
    public Result pageTeacherQuery(
            @ApiParam(name = "current" ,value = "当前页") @PathVariable("current") Long current,
            @ApiParam(name = "size" ,value = "页面大小") @PathVariable("size") Long size,
            @RequestBody(required = false) TeacherQuery teacherQuery) {
        Page<EduTeacher> eduTeacherPage = new Page<>(current,size);

        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //构造查询条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",teacherQuery.getName());
        }
        if (!StringUtils.isEmpty(level)){
            wrapper.eq("level",teacherQuery.getLevel());
        }
        if (!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",teacherQuery.getBegin());
        }
        if (!StringUtils.isEmpty(end)){
            wrapper.le("gmt_modified",teacherQuery.getEnd());
        }

        wrapper.orderByDesc("gmt_create");


        eduTeacherService.page(eduTeacherPage,wrapper);

        long total = eduTeacherPage.getTotal();
        List<EduTeacher> records = eduTeacherPage.getRecords();
        return Result.ok().data("total",total).data("records",records);
    }

    //添加讲师
    @ApiOperation("添加讲师")
    @PostMapping("/addTeacher")
    public Result addTeacher(@RequestBody EduTeacher eduTeacher){
        if (eduTeacherService.save(eduTeacher)){
            return Result.ok();
        }else {
            return Result.error();
        }
    }


    //根据Id查询讲师
    @ApiOperation("根据Id查询讲师")
    @GetMapping("/getTeacher/{id}")
    public Result getTeacherById(@ApiParam(name = "id" ,value = "讲师ID") @PathVariable("id") String id){
        return Result.ok().data("teacher",eduTeacherService.getById(id));
    }


    //修改讲师
    @ApiOperation("修改讲师")
    @PostMapping("/updateTeacher")
    public Result updateTeacher(@RequestBody EduTeacher eduTeacher){
        System.out.println(eduTeacher);
        if (eduTeacherService.updateById(eduTeacher)){
            return Result.ok();
        }else{
            return Result.error();
        }
    }

}

